import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import homeR from "./modules/homeR.js";
Vue.use(VueRouter)

const routes = [{
        path: '/',
        name: 'home',
        component: HomeView,
        redirect: "/admin/user/level",
        children: homeR,
        meta: {
            isTop: true
        }
    },
    {
        path: '/about',
        name: 'about',
        component: () =>
            import ('../views/AboutView.vue')
    },
    {
        path: '/login',
        name: 'login',

        component: () =>
            import ('../views/login.vue')
    }
    // ...userRouter,
]

const router = new VueRouter({
    mode: "history",
    routes
})
router.beforeEach((to, from, next) => {
    // 先判断当前路由是否需要拦截
    if (to.matched.length == 0) {
        next('/err')
    } else {
        if (to.matched.some(v => { return v.meta.isTop == true })) {
            if (sessionStorage.getItem("token")) {
                next()
            } else {
                next("/login")
            }
        } else {
            next()
        }
    }
})
export default router